﻿
/**
* Masque JS Library 0.0.1.7
* Copyright(c) 2008, 青岛领创网络科技有限公司
* @author Lookingon Team
*/
if (window.Masque)
    _Masque = window.Masque;

var Masque = window.Masque = {
    version: '0.0.1',
    supportW3C: (document.getElementById) ? true : false,
    isOpera: window.opera && navigator.userAgent.match(/opera/gi) ? true : false,
    isIE: !this.isOpera && document.all && navigator.userAgent.match(/msie/gi) ? true : false,
    isIE5: (this.supportW3C && this.isIE) ? true : false,
    isIE6: navigator.userAgent.toLowerCase().indexOf('msie 6') > -1,
    isNS: (navigator.appName == "Netscape") ? true : false,
    isSafari: !this.isIE && navigator.userAgent.match(/safari/gi) ? true : false,
    isGecko: !this.isIE && navigator.userAgent.match(/gecko/gi) ? true : false,
    isFirefox: !this.isIE && navigator.userAgent.match(/firefox/gi) ? true : false,
    isFF3: !this.isIE && navigator.userAgent.match(/Firefox\/3/gi) ? true : false,

    V: {
        isElement: function(object) {
            return object && object.nodeType == 1;
        },
        isFunction: function(object) {
            return typeof object == "function";
        },

        isString: function(object) {
            return typeof object == "string";
        },

        isNumber: function(object) {
            return typeof object == "number";
        },

        isUndefined: function(object) {
            return typeof object == "undefined";
        },
        isObject: function(object) {
            return typeof object == "object";
        }
    }


};
//应加入错误处理，捕获window错误，如果是本JS类库的错误，可以进行处理，如果缺失JS文件引用，引入文件并转回执行，不知是否可行。


Object.extend = function(destination, source) {
    for (var property in source)
        destination[property] = source[property];
    return destination;
};

if (!Array.prototype.push) {
    Array.prototype.push = function() {
        var startLength = this.length;
        for (var i = 0; i < arguments.length; i++)
            this[startLength + i] = arguments[i];
        return this.length
    }
};

var Try = {
    these: function() {
        var returnValue;

        for (var i = 0, length = arguments.length; i < length; i++) {
            var lambda = arguments[i];
            try {
                returnValue = lambda();
                break;
            }
            catch (e) {
            }
        }

        return returnValue;
    }
};


function $(element) {

    /* Var 0.0.0.1
    var elements=new Array();
    for(var i=0;i<arguments.length;i++)
    {
    var element=arguments[i];
    if(typeof element=='string')
    element=document.getElementById(element);
    if(arguments.length==1)
    return element;
    elements.push(element)
    };
    return elements
    */
    if (arguments.length > 1) {
        for (var i = 0, elements = [], length = arguments.length; i < length; i++)
            elements.push($(arguments[i]));
        return elements;
    }
    if (Masque.V.isString(element))
        element = document.getElementById(element);
    else if (element != null && (element.parentNode == null || element.parentNode.nodeName == "#document-fragment"))
        element = document.getElementById(element.id);

    return element;
}

/*document Extend-------------------------------------------------------*/
document.getElementsByClassName = function(className, parentElement) {
    var children = ($(parentElement) || document.body).getElementsByTagName('*');
    var elements = new Array();
    for (var i = 0; i < children.length; i++) {
        if (children[i].className == className) {
            elements.push(children[i]);
        }
    }
    return elements;
}



/**
* X-browser event handler attachment and detachment
* TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
*
* @argument obj - the object to attach event to
* @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
* @argument fn - function to call
*/
function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    }
    else
        if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    }
    else {
        return false;
    }
}

function removeEvent(obj, evType, fn, useCapture) {
    if (obj.removeEventListener) {
        obj.removeEventListener(evType, fn, useCapture);
        return true;
    }
    else
        if (obj.detachEvent) {
        var r = obj.detachEvent("on" + evType, fn);
        return r;
    }
    else {
        alert("Handler could not be removed");
    }
}

Function.prototype.bind = function(object) {
    var __method = this;
    return function() {
        __method.apply(object, arguments)
    }
};
Function.prototype.bindAsEventListener = function(object) {
    var __method = this;
    return function(event) {
        __method.call(object, event || window.event)
    }
};


function GetPosition(node, pOffsetParent) {
    var top = 0;
    var left = 0;

    var isAbsolute = false;
    var offsetParent = node;
    while (offsetParent != null && offsetParent != pOffsetParent && offsetParent != document.body) {
        top += offsetParent.offsetTop;
        left += offsetParent.offsetLeft;
        if (Masque.isIE6) {
            if (offsetParent.currentStyle.position == 'absolute' && !isAbsolute) {
                isAbsolute = true;
            }
        }
        if (!Masque.isNS) {
            parseInt(offsetParent.currentStyle.borderTopWidth) > 0 ? top += parseInt(offsetParent.currentStyle.borderTopWidth) : 0;
            parseInt(offsetParent.currentStyle.borderLeftWidth) > 0 ? left += parseInt(offsetParent.currentStyle.borderLeftWidth) : 0;

            if (offsetParent.scrollTop > 0 && offsetParent.nodeName.toLowerCase() != 'html') {

                top -= offsetParent.scrollTop;
                left -= offsetParent.scrollLeft;
            }
        }
        offsetParent = offsetParent.offsetParent;
    }
    if (Masque.isNS) {
        var offsetParent = node;
        while (offsetParent != null && offsetParent != pOffsetParent && offsetParent != document.body) {
            if (offsetParent.scrollTop > 0 && offsetParent.nodeName.toLowerCase() != 'html') {
                top -= offsetParent.scrollTop;
            }
            if (offsetParent.scrollLeft > 0 && offsetParent.nodeName.toLowerCase() != 'html') {
                left -= offsetParent.scrollLeft;
            }
            offsetParent = offsetParent.parentNode;
        }
    }

    if (Masque.isIE6) {
        if (isAbsolute) {
            top = top - parseInt(node.document.body.topMargin, 10)
            left = left - parseInt(node.document.body.leftMargin, 10)
        }
        else {
            top += node.document.body.offsetTop;
            left += node.document.body.offsetLeft;
        }
    } else if (!Masque.isNS) {
        if (Masque.isIE) {
            if (offsetParent != null && offsetParent != pOffsetParent) {
                top += offsetParent.offsetTop;
                left += offsetParent.offsetLeft;
            }
        }
        else {
            top += parseInt(node.document.body.topMargin, 10);
            left += parseInt(node.document.body.left, 10);
        }
    }
    return { "Top": top, "Left": left };
}




/*String Extend-------------------------------------------------------*/
Object.extend(String.prototype, {
    escape: function() {
        return escape(this);
    },
    unescape: function() {
        return unescape(this);
    },
    encodeURI: function() {
        return encodeURI(this);
    },
    decodeURI: function() {
        return decodeURI(this);
    },
    encodeURIComponent: function() {
        return encodeURIComponent(this);
    },
    decodeURIComponent: function() {
        return decodeURIComponent(this);
    },
    trim: function() {
        var temp = this;
        if (arguments.length == 0) {
            temp = temp.replace(/\s*$/gi, '');
            temp = temp.replace(/^\s*/gi, '');
            return temp;
        }
        else {
            var reg = new RegExp('^' + arguments[0], 'g');
            temp = temp.replace(reg, '');
            reg = new RegExp(arguments[0] + '$', 'g');
            temp = temp.replace(reg, '');
            return temp;
        }
    },
    trimStart: function() {
        var temp = this;
        if (arguments.length == 0) {
            temp = temp.replace(/^\s*/gi, '');
            return temp;
        }
        else {
            var reg = new RegExp('^' + arguments[0], 'g');
            temp = temp.replace(reg, '');
            return temp;
        }
    },
    trimEnd: function() {
        var temp = this;
        if (arguments.length == 0) {
            temp = temp.replace(/\s*$/gi, '');
            return temp;
        }
        else {
            var reg = new RegExp(arguments[0] + '$', 'g');
            temp = temp.replace(reg, '');
            return temp;
        }
    },
    translateHTML: function() {
        var temp = this;
        temp = temp.replace(/</g, '&lt;');
        temp = temp.replace(/>/g, '&gt;');
        temp = temp.replace(/\n/g, '<br />');
        temp = temp.replace(/\t/g, '&nbsp;&nbsp;&nbsp;');
        temp = temp.replace(/"/g, '&quot;');
        temp = temp.replace(/&/g, '&amp;');
        return temp;
    }, format: function() {
        var input = this;
        if (input == null || input.length < 1) {
            return '';
        }
        var temp = input;
        var index = 0;
        var reg;
        while (true) {
            if (temp.indexOf('{' + index + '}') > -1) {
                reg = new RegExp('\\{' + index + '\\}', 'gi');
                temp = temp.replace(reg, arguments[index]);
            }
            else {
                break;
            }
            index++;
        }
        return temp;
    },
    length: function() {
        return length(this);
    },

    splitEx: function(trimSpace) {
        var str = this;
        var a = new Array();
        if (str == null || str.length < 1) {
            return a;
        }
        while (str.length > 0) {
            var index = -1;
            for (var i = 1; i < arguments.length; i++) {
                var j = str.indexOf(arguments[i]);
                if (j != -1) {
                    if (index == -1)
                        index = j;
                    else
                        index = Math.min(index, j);
                }
            }

            if (index == -1) {
                a.push(str);
                str = "";
            } else {
                var t = str.substring(0, index);
                if (trimSpace == true) {
                    t = t.trim();
                }
                if (t.length > 0)
                    a.push(t);
                str = str.substring(index + 1);
            }
        }
        return a;


    }
});

function GetEvent() {     //同时兼容ie和ff的写法
    if (document.all) return window.event;
    func = this.caller;
    while (func != null) {
        var arg0 = func.arguments[0];
        if (arg0) {
            if ((arg0.constructor == Event || arg0.constructor == MouseEvent)
                     || (typeof (arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)) {
                return arg0;
            }
        }
        func = func.caller;
    }
    return null;
}

//以上通过验证

function __FireFox() {
    HTMLElement.prototype.__defineGetter__("runtimeStyle", __element_style);
    window.constructor.prototype.__defineGetter__("event", __window_event);
    Event.prototype.__defineGetter__("srcElement", __event_srcElement);
}

function __element_style() {
    return this.style;
}

function __window_event() {
    return __window_event_constructor();
}

function __window_event_constructor() {
    var i = 0;
    var _caller = __window_event_constructor.caller;
    while (_caller != null && i < 50) {
        var _argument = _caller.arguments[0];
        if (_argument) {
            var _ac = _argument.constructor;
            if (_ac.toString().indexOf("Event") != -1) {
                return _argument;
            }
        }
        _caller = _caller.caller;
        i++;
    }
    return null;
}

function __event_srcElement() {
    return this.target;
}

if (!document.all) {
    __FireFox();
}

function GetKeyCode(e) {
    var keynum = "";
    if (Masque.isIE) // window.event IE
    {
        keynum = e.keyCode;
    }
    else // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }
    return keynum;
}

//if (!window.Event) {
//    var Event = new Object()
//};
//Object.extend(Event, {
//    observers: false,
//    element: function(event) {
//        return event.target || event.srcElement
//    },
//    isLeftClick: function(event) {
//        return (((event.which) && (event.which == 1)) || ((event.button) && (event.button == 1)))
//    },
//    pointerX: function(event) {
//        return event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft))
//    },
//    pointerY: function(event) {
//        return event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop))
//    },
//    stop: function(event) {
//        if (event.preventDefault) {
//            event.preventDefault();
//            event.stopPropagation()
//        }
//        else {
//            event.returnValue = false;
//            event.cancelBubble = true
//        }
//    },
//    findElement: function(event, tagName) {
//        var element = Event.element(event);
//        while (element.parentNode && (!element.tagName || (element.tagName.toUpperCase() != tagName.toUpperCase())))
//            element = element.parentNode;
//        return element
//    },
//    _observeAndCache: function(element, name, observer, useCapture) {
//        if (!this.observers)
//            this.observers = [];
//        if (element == null) return;
//        if (element.addEventListener) {
//            this.observers.push([element, name, observer, useCapture]);
//            element.addEventListener(name, observer, useCapture)
//        }
//        else
//            if (element.attachEvent) {
//            this.observers.push([element, name, observer, useCapture]);
//            element.attachEvent('on' + name, observer)
//        }
//    },
//    unloadCache: function() {
//        if (!Event.observers)
//            return;
//        for (var i = 0; i < Event.observers.length; i++) {
//            Event.stopObserving.apply(this, Event.observers[i]);
//            Event.observers[i][0] = null
//        };
//        Event.observers = false
//    },
//    observe: function(element, name, observer, useCapture) {
//        var element = $(element);
//        useCapture = useCapture || false;
//        if (name == 'keypress' && (navigator.appVersion.match(/Konqueror|Safari|KHTML/) || element.attachEvent))
//            name = 'keydown';
//        this._observeAndCache(element, name, observer, useCapture)
//    },
//    stopObserving: function(element, name, observer, useCapture) {
//        var element = $(element);
//        useCapture = useCapture || false;
//        if (name == 'keypress' && (navigator.appVersion.match(/Konqueror|Safari|KHTML/) || element.detachEvent))
//            name = 'keydown';
//        if (element.removeEventListener) {
//            element.removeEventListener(name, observer, useCapture)
//        }
//        else
//            if (element.detachEvent) {
//            element.detachEvent('on' + name, observer)
//        }
//    }
//});
//Event.observe(window, 'unload', Event.unloadCache, false);



var Class = function() {
    var _class = function() {

        return Object.extend(this, _class).initialize.apply(this, arguments);
    };
    for (i = 0; i < arguments.length; i++) {
        superClass = arguments[i];
        _class.extend(superClass.prototype);
    }
    Object.extend(_class, {
        child: function() {
            return new Class(this);
        },
        initialize: function(options) {
            this.SetOption(options);
        },
        extend: function(f) {
            Object.extend(this, f);
        },
        option: {},
        SetOption: function(options) {
            Object.extend(this.option, options || {});
        }
    });
    return _class;
};





